<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <script type="text/javascript" src="../static/js/add/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            // test();
            $("#getOrder").click(function () {
                getOrder();
            })


        })


        function login() {
            $.ajax({
                type: "POST",
                url: "http://localhost:8080/user/login",
                data: {"username": "admin", "password": "111"},
                dataType: "json",
                // crossDomain: true,
                xhrFields: {withCredentials: true},
                success: function (data) {
                    $("#show").text(data.user.userName);
                }
            });
        }

        function getOrder() {
            $.ajax({
                type: "GET",
                url: "http://localhost:80/order/order/getOrder",
                // data:{"username":"admin","password":"111"},
                dataType: "json",
                // crossDomain: true,
                xhrFields: {withCredentials: true},
                success: function (data) {
                    $(data.records).each(function () {
                        alert(this.orderId);
                    });
                }
            });
        }

        function selectShippingAddress() {
            $.ajax({
                type: "GET",
                url: "http://localhost:80/user/shipping-address/selectShippingAddress",
                dataType: "json",
                xhrFields: {withCredentials: true},
                success: function (data) {
                    $(data).each(function (index, value) {
                        var receiverProvince = this.receiverProvince;
                        var receiverCity = this.receiverCity;
                        var receiverDistrict = this.receiverDistrict;
                        alert(receiverProvince + receiverCity + receiverDistrict);
                    })
                }
            });
        }

        function getSkuText() {
            $.ajax({
                type: "GET",
                url: "http://localhost:80/goods/goodssku/getSkuText",
                data: {"skuId": 1},
                dataType: "json",
                xhrFields: {withCredentials: true},
                success: function (data) {
                    $(data).each(function (skuIndex, skuText) {
                        $(skuText).each(function (attrIndex, attrText) {
                            // 如果attrIndex是0代表是属性的名字，可以单独处理（加粗等操作）;否则就是属性的值
                            if (attrIndex == 0) {
                                console.error(attrText);
                            } else {
                                console.log(attrText);
                            }
                        });
                    });
                }
            });
        }


        function createOrderInfo() {
            // 首先要有对象（此对象不是JSON对象）
            var selectGoods = [{"skuId": 1, "goodsId": 1, "quantity": 1}, {"skuId": 5, "goodsId": 2, "quantity": 2}];
            // selectGoods.push({"skuId": 1, "goodsId": 1, "quantity": 1});
            // selectGoods.push({"skuId": 5, "goodsId": 2, "quantity": 2});
            // 将对象序列化成JSONSTRING
            var list = JSON.stringify(selectGoods);
            $.ajax({
                type: 'POST',
                url: 'http://localhost/order/order/createOrderInfo',
                // 指定发送请求的格式
                contentType: 'application/json',
                data: list,
                dataType: 'json',
                xhrFields: {withCredentials: true},
                success: function (data) {

                }
            });
        }

        var emp = new Array();

        function checkboxOnclick(checkbox) {
            if (!window.localStorage) {
                alert("浏览器支持localstorage");
                return false;
            } else {
                //主逻辑业务
                var storage = window.localStorage;
                if (checkbox.checked == true) {
                    emp.push(checkbox.value);
                    console.log(emp);
                } else {
                    var index = emp.indexOf(checkbox.value);
                    emp.splice(index, 1);
                    console.log(emp);
                }
                storage.setItem("cartId", JSON.stringify(emp));
                console.log("cartId:" + localStorage.getItem('cartId'));
            }

        }

        var order = new Array();

        // 创建订单
        function createOrder() {
            // order.push({
            //     "payment": 73.21,
            //     "shippingId": 1,
            //     "orderDtl": {
            //         "goodsId": 1,
            //         "skuId": 1,
            //         "quantity": 1
            //     }
            // });
            // order.push({
            //     "payment": 23.00,
            //     "shippingId": 1,
            //     "orderDtl": {
            //         "goodsId": 2,
            //         "skuId": 5,
            //         "quantity": 1
            //     }
            // });
            if (!window.localStorage) {
                alert("浏览器支持localstorage");
                return false;
            } else {
                var storage = window.localStorage;
                var cartID = JSON.parse(storage.getItem('cartId'));
                var urlParam = '?';
                for (var i = 0; i < cartID.length; i++) {
                    if (i > 0) {
                        urlParam = urlParam + '&';
                    }
                    urlParam = urlParam + 'cartId=' + cartID[i];
                }
            }
            var orderJson = JSON.stringify(order);
            $.ajax({
                type: 'POST',
                url: 'http://localhost:8082/order/createOrder' + urlParam,
                // 指定发送请求的格式
                contentType: 'application/json',
                data: orderJson,
                dataType: 'text',
                xhrFields: {withCredentials: true},
                success: function (data) {
                    $("#payment").append(data);
                    console.log(data);
                }
            });
        }

        /**
         * 1. 进入页面调用clearing(),从localStorage中读取CartId，Ajax调用后台接口拿到商品信息，
         * 2. 调用
         * @returns {boolean}
         */
        // 生成订单信息
        function clearing() {
            if (!window.localStorage) {
                alert("浏览器支持localstorage");
                return false;
            } else {
                var storage = window.localStorage;
                var cartID = JSON.parse(storage.getItem('cartId'));
                console.log(cartID);
                // var urlParam = '?';
                // for(var i=0;i<cartID.length;i++){
                //     if (i > 0){
                //         urlParam = urlParam + '&';
                //     }
                //     urlParam = urlParam + 'cartId=' + cartID[i];
                // }

                $.ajax({
                    type: 'POST',
                    url: 'http://localhost:8082/order/clearing',
                    // 指定发送请求的格式
                    // contentType : 'application/json',
                    data: {"cartId": cartID},
                    dataType: 'json',
                    xhrFields: {withCredentials: true},
                    success: function (data) {
                        // 1. 将信息在页面展示

                        // 2. 把需要验证的信息放在order变量中
                        $(data).each(function (index, value) {
                            var oneOrder = {
                                "payment": value.totalPrice,
                                "orderDtl": {
                                    "goodsId": value.goodsId,
                                    "skuId": value.skuId,
                                    "quantity": value.quantity
                                }
                            };
                            order.push(oneOrder);
                        });
                        addShippingAddress();
                        console.log(JSON.stringify(order));
                    }
                });
            }
        }

        // 往order中添加收货地址
        function addShippingAddress() {
            $(order).each(function (index, value) {
                value.shippingId = 1;
            });
            console.log(JSON.stringify(order));
        }
    </script>
</head>
<body>
<!--<form method="post" action="http://localhost:8080/user/baseinfo/uploadUserPhoto" enctype="multipart/form-data">-->
<!--    <input type="file" name="file">-->
<!--    <input type="submit" value="上传">-->
<!--</form>-->

<input type="button" value="以ajax(post)方式获取用户名" onclick="login()"/>
<br/>
用户名:
<span id="show"></span>
<br/>
<span id="getOrder">获取用户订单</span>
<!--    <input type="button" value="获取用户订单" onclick="getOrder()" />-->
<br/>
<input type="button" value="查询收获地址" onclick="selectShippingAddress()">

<br/>
<input type="button" value="显示sku的属性和值" onclick="getSkuText()">

<br/>
<input type="button" value="生成订单" onclick="createOrderInfo()">

<br/>
<input type="checkbox" value="1" onclick="checkboxOnclick(this)">1
<input type="checkbox" value="2" onclick="checkboxOnclick(this)">2
<input type="checkbox" value="4" onclick="checkboxOnclick(this)">4
<input type="checkbox" value="3" onclick="checkboxOnclick(this)">3
<input type="checkbox" value="7" onclick="checkboxOnclick(this)">7

<br/>

<input type="button" value="生成订单信息" onclick="clearing()">

<br/>
<input type="button" value="创建订单并付款" onclick="createOrder()">

<div id="payment">

</div>

<div id="orderInfo">

</div>

</body>
</html>